package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysGroup;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户组
 *
 * @author zyh
 */
@RestController
@RequestMapping("/system/group")
public class SysGroupController extends BaseController
{

    @Autowired
    private ISysGroupService sysGroupService;

    /**
     * 分页
     * @param sysGroup
     * @return
     */
    @GetMapping("/page")
    public TableDataInfo list(SysGroup sysGroup)
    {
        startPage();
        List<SysGroup> list = sysGroupService.selectGroupList(sysGroup);

        return getDataTable(list);
    }

    /**
     * 新增用户组
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysGroup sysGroup)
    {
        int row = sysGroupService.saveGroup(sysGroup);
        return toAjax(row);
    }


    /**
     * 查看详情
     * @param groupId
     * @return
     */
    @GetMapping("/info/{groupId}")
    public AjaxResult getInfo(@PathVariable("groupId") Long groupId) {
        SysGroup group = sysGroupService.getGroupById(groupId);
        return AjaxResult.success(group);
    }

    /**
     * 编辑用户组
     * @param sysGroup
     * @return
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysGroup sysGroup)
    {
        int row = sysGroupService.updateGroup(sysGroup);
        return toAjax(row);
    }

    @DeleteMapping("/{groupIds}")
    public AjaxResult delete(@PathVariable("groupIds") Long[] groupIds) {
        int row = sysGroupService.deleteGroup(groupIds);
        return toAjax(row);
    }
}
